package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.ljfiction.book.Baner.MyBookBean;
import com.ljfiction.book.R;
import com.ljfiction.book.adapter.MyBookAdapter;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserManageBean;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class CreatBookActivity extends BaseActivity {

    @BindView(R.id.daohanglan_creatbook)
    LinearLayout daohanglanCreatbook;

    @BindView(R.id.nobooks_mybook)
    LinearLayout nobooksMybook;


    @BindView(R.id.recycleview_mybooks)
    RecyclerView recycleviewMybooks;
    private List<MyBookBean.DataBean> booklist = new ArrayList<>();
    private MyBookBean.DataBean addbook;
    private MyBookAdapter mAdapter;
    private Intent intent;
    private MyBookBean myBookBean;
    private Intent intent1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creat_book);
        ButterKnife.bind(this);
        todaohanglanHeight(daohanglanCreatbook, this);
        addbook = new MyBookBean.DataBean();
        intent = new Intent(this, WriterActivity.class);
        intent1 = new Intent(this, MyBookActivity.class);
        intview();
        intData();
    }

    private void intview() {
        mAdapter = new MyBookAdapter(R.layout.mybook_item, booklist);
        recycleviewMybooks.setAdapter(mAdapter);
        recycleviewMybooks.setLayoutManager(new GridLayoutManager(this, 3));
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (position == 0) {
                    startActivity(intent);
                } else {
                    intent1.putExtra("bookId", String.valueOf(booklist.get(position).getBookId()));
                    startActivity(intent1);
                }
            }
        });
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/book-AuthorAllBookInfo")
                .params("authorId", String.valueOf(UserManageBean.userBean.getUserId()))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        try {
                            myBookBean = gson.fromJson(s, MyBookBean.class);
                            if (myBookBean.getReturnCode() == 200) {
                                if (myBookBean.getData().size() > 0) {
                                    booklist.clear();
                                    booklist.add(addbook);
                                    booklist.addAll(myBookBean.getData());
                                    recycleviewMybooks.setVisibility(View.VISIBLE);
                                    nobooksMybook.setVisibility(View.GONE);
                                    mAdapter.notifyDataSetChanged();
                                } else {
                                    recycleviewMybooks.setVisibility(View.GONE);
                                    nobooksMybook.setVisibility(View.VISIBLE);
                                }
                            } else {
                                recycleviewMybooks.setVisibility(View.GONE);
                                nobooksMybook.setVisibility(View.VISIBLE);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

    }


    @Override
    protected void onRestart() {
        super.onRestart();
        intData();
    }

    @OnClick({R.id.back_addWriter, R.id.creatbook_mybook})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_addWriter:
                goBack();
                break;
            case R.id.creatbook_mybook:
                startActivity(intent);
                break;
        }
    }
}
